package com.littlebee.storysuper.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.littlebee.storysuper.R;
import com.littlebee.storysuper.activity.EnglishInfoActivity;
import com.littlebee.storysuper.activity.StoryInfoActivity;
import com.littlebee.storysuper.activity.TodayStoryInfoActivity;
import com.littlebee.storysuper.entry.BabyStorySeriesEntry;
import com.littlebee.storysuper.entry.EnglishCartoonsEntry;
import com.littlebee.storysuper.entry.EnglistListEntry;
import com.littlebee.storysuper.entry.StorySeriesEntry;
import com.littlebee.storysuper.widget.NoScrollGridView;

import java.util.List;

public class CircleEnglistAdapter extends BaseAdapter {

    private List<EnglishCartoonsEntry> mCircleBeans;
    private Context mContext;
    private LayoutInflater mLayoutInflater;

    public CircleEnglistAdapter(List<EnglishCartoonsEntry> beans, Context context) {
        this.mCircleBeans = beans;
        this.mContext = context;
        mLayoutInflater = LayoutInflater.from(context);

    }

    @Override
    public int getCount() {
        return mCircleBeans.size();
    }

    @Override
    public Object getItem(int position) {
        return mCircleBeans.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = mLayoutInflater.inflate(R.layout.item_listview_circle_story,parent, false);
            holder = new ViewHolder();
            holder.gridView = (NoScrollGridView) convertView.findViewById(R.id.gridView);
            holder.tv_nickname = (TextView) convertView.findViewById(R.id.tv_nickname);
            holder.tv_more = (TextView) convertView.findViewById(R.id.tv_more);
            holder.tv_more.setVisibility(View.INVISIBLE);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        final EnglishCartoonsEntry bean = (EnglishCartoonsEntry) getItem(position);
        holder.tv_more.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(mContext, TodayStoryInfoActivity.class);
                intent.putExtra("title", bean.getCartoontypename());
                intent.putExtra("id", bean.getCartoontypeid());
                mContext.startActivity(intent);
            }
        });
        if (null != bean) {

            // 设置时间
            holder.tv_nickname.setText(bean.getCartoontypename());
            // 判断如果用户是否上传图片
            if (bean.getCartoonseries() != null && bean.getCartoonseries().size() > 0) {
                // 有：设置Adapter显示图片
                holder.gridView.setVisibility(View.VISIBLE);
                holder.gridView.setAdapter(new CircleGridEnglistAdapter(bean.getCartoonseries(), mContext));
                // 设置点击事件
                holder.gridView.setOnItemClickListener(new OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> parent,
                                            View view, int position, long id) {
                        enterPhotoDetailed(bean.getCartoonseries().get(position));
                    }
                });
            } else {
                // 否：隐藏
                holder.gridView.setVisibility(View.GONE);
            }
        }
        return convertView;
    }

    /**
     * 进入图片详情页
     *
     */
    protected void enterPhotoDetailed(EnglistListEntry storySeriesEntry) {
        Intent intent = new Intent(mContext, EnglishInfoActivity.class);
        intent.putExtra("info", storySeriesEntry);
        mContext.startActivity(intent);
    }

    private static class ViewHolder {
        TextView tv_nickname;
        TextView tv_more;
        NoScrollGridView gridView;
    }
}